package com.scfinance.SPD.tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created by admin on 2017/7/4.
 */
public class Comm {

    public static Logger logger = LoggerFactory.getLogger(Comm.class);

    /**
     * 截取身份证上的出生日期
     **/
    public static String getBirthday(String idCard) {
        String birthday = idCard.substring(6, 14);
        return birthday;
    }

    /**
     * 截取身份证上的性别信息
     */
    public static String convertSex(String idNo) {
        String sex = "M";
        try {
            String flag = idNo.substring(16, 17);
            if (Integer.parseInt(flag) % 2 == 0) {
                sex = "F";
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("截取性别信息出错" + e);
        }
        return sex;
    }

//    /**
//     * 分期的相关参数映射
//     */
//    public static UploadAppReq termInfo(UploadAppReq uploadAppReq, String number, String type) {
//        try {
//            JSONObject termJson = Term.getTermJson();
//            JSONObject termDetail = termJson.getJSONObject(String.valueOf(type)).getJSONObject(number);
//            String prdCode = termDetail.getString("qfproductcode");
//            String fee = termDetail.getString("qffenqifeilv");
//
//            uploadAppReq.setQffenqiqishu(number);
//            uploadAppReq.setQffenqitype(type);
//            uploadAppReq.setQfproductcode(prdCode);
//            uploadAppReq.setQffenqifeilv(fee);
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error("分期相关转化出错" + e);
//        }
//
//        return uploadAppReq;
//    }

    public static JSONObject readJson(String path) {
        JSONObject json = new JSONObject();
        InputStream inStream = Comm.class.getResourceAsStream(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            json = JSONObject.parseObject(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return json;
    }

    public static JSONArray readJsonArray(String path) {
        JSONArray jsonArray = new JSONArray();
        InputStream inStream = Comm.class.getResourceAsStream(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            jsonArray = JSONArray.parseArray(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonArray;
    }
}
